<?php 
include("header.php");
include("navigation.php");
include("database.php");
?>

    <!-- Columns -->
    <div class="cols-top"></div>
    <div class="cols box">

        <!-- Content -->
        <div class="content">
        
            
            <?php if (isset($_SESSION['nick'])){ ?>
            <p> 
            <!-- Perex -->
            <div class="perex">
			
                <p>Mi cuenta</p>
				
            </div> <!-- /perex -->
            
            <div class="demo">
				<div id="tabs-default">
					<div id="tabs-default-1">
						<h3>General</h3>
							<p><?php 
								$avatar = $_REQUEST['upload'];
								$nombre = $_REQUEST['nombre'];
								$apellido = $_REQUEST['apellidos'];
								$web = $_REQUEST['url'];
										
								if($nombre != NULL && $apellido != NULL){
									$guardar = "design/avatar/".$_FILES['upload']['name'];
									if(move_uploaded_file($_FILES['upload']['tmp_name'], $guardar)) {
							
										$origen = "design/avatar/".$_FILES['upload']['name'];
										$extension = strtolower(array_pop(explode(".",$_FILES['upload']['name'])));
										$destino = "design/avatar/". $_SESSION['nick'] . "." . $extension;
										$avatar_new = rename($origen, $destino);
							
										include("database.php");
							
										$destino = $_SESSION['nick'] . "." . $extension;
										$consulta = "UPDATE usuario SET avatar = '" . $destino . "' WHERE nick = '" . $_SESSION['nick'] . "';";
										$enviar = mysql_query($consulta, $enlace);
										$enviar2 = mysql_fetch_array($enviar);
										echo "<div class=\"content-box box\"> Cambios realizados </div>";
									}
						
									include("database.php");
									$pass_cod = md5($password);
									$consulta = "UPDATE usuario SET nombre = '" . $nombre . "', apellido = '" . $apellido . "', web = '" . $web . "' WHERE nick = '" . $_SESSION['nick'] . "';";
									$enviar = mysql_query($consulta, $enlace);
									$enviar2 = mysql_fetch_array($enviar);
									echo "<div class=\"content-box box\"> Cambios realizados </div>";
								}
									
				
							?>
							<?php //Cambio de claves
								$pass1 = $_REQUEST['pass1'];
								$pass2 = $_REQUEST['pass2'];
								$antpass = $_REQUEST['antpass'];
								$nick = $_SESSION['nick'];
								
								$antpass_md5 = md5($antpass);
								$pass1_md5 = md5($pass1);
								
								include("database.php");				
								$comprobar_pass = "SELECT * FROM usuario WHERE nick = '" . $nick . "'";
								$enviar_consulta = mysql_query($comprobar_pass, $enlace);
								$dato = mysql_fetch_array($enviar_consulta);
								if($pass1 != NULL){
									if($dato['password'] == $antpass_md5){
										if($pass1 == $pass2){
											include("database.php");
											$guardar_clave = "UPDATE usuario SET password = '" . $pass1_md5 . "' WHERE nick = '" . $nick . "';";
											$enviar_consulta = mysql_query($guardar_clave, $enlace);
											$enviar_consulta2 = mysql_fetch_array($enviar_consulta);
											echo "Nueva clave guardada!";
										
										}else echo "Los campos no coinciden!";
									}else echo "Tu contrase&ntilde;a no es correcta!";
								}
							?>
							
							<?php //Datos personales
								$nick = $_SESSION['nick'];
								$consulta=mysql_query("SELECT * FROM usuario WHERE nick = '$nick'",$enlace);
				
								if(mysql_num_rows($consulta) != 0){
			
									for($inicio=1; $dato = mysql_fetch_array($consulta); $inicio+=1){
							?>
					
										<div class="content-box box">
            
											<p class="nom t-left">
                    	
												<form action="" method="post" enctype="multipart/form-data">
                    		
													<div class="content-box box">
														<img src="design/avatar/<?php echo $dato['avatar'];?>" height="128" width="128" />
                				
														<input name="upload" type="file">
														<p class="nomt"><strong>Nombre:</strong><br />
														<input type="text" size="65" class="input" name="nombre" value="<?php echo $dato['nombre'];?> " /></p>

														<p><strong>Apellidos:</strong><br />
															<input type="text" size="65" class="input" name="apellidos" value="<?php echo $dato['apellido'];?>" /></p>
                    			
														<p><strong>Web:</strong><br />
															<input type="text" size="65" class="input" name="url" value="<?php echo $dato['web'];?>" /></p>

															<p class="nomb t-center"><input type="image" src="design/btn-ok.gif" value="Guardar Cambios" /></p>

													</div> <!-- /content-box -->
												</form>
					
										</div> <!-- /content-box -->
						<?php 
								}//fin del for
							}//fin del if
							else{
								echo "Error";		
							}
						?>
					</p>
        </div>
        <div id="tabs-default-2">
          <h3>Clave</h3>
          <p>
			
			<form action="user_config.php" method="post">
                <div class="content-box box">

                    <p class="nomt"><strong>Nueva contrase&ntilde;a:</strong><br />
                    <input type="password" size="65" name="pass1" class="input" /></p>

                    <p><strong>Repite la contrase&ntilde;a:</strong><br />
                    <input type="password" size="65" name="pass2" class="input" /></p>

                    <p>Tu contrase&ntilde;a antigua:<br />
                    <input type="password" size="65" name="antpass" class="input" /></p>

                    <p class="nomb t-center"><input type="image" src="design/btn-ok.gif" value="OK" /></p>

                </div> <!-- /content-box -->
            </form>

          </p>
        </div>
        
      </div>
    </div>
            
		

           <?php 
			} 
			else {
				echo "P&aacute;gina no v&aacute;lida. Prueba a loguearte primero o <a href=\"register.php\">registrarte</a>";
			}
			?>
        </div> <!-- /content -->
        

       <?php
       		include("sidebar.php");
       ?>

    <!-- Footer -->
    <?php include("footer.php");?> 
    <!-- /footer -->

</div> <!-- /main -->

</body>
</html>
